﻿Imports System.Xml
Imports System.IO
Public Class PreReq

    Private Sub PreReq_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 2
        ProgressBar1.Value = 0
        Try
            If Not Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\") Then
                Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\")
            End If
        Catch ex As Exception
            MsgBox("Failed to create WSI directory in \Appdata\Local")
        End Try
        Try
            Me.createUser()
            ProgressBar1.Value = 1
            Label2.Text = "Creating Default User Config..."
        Catch ex As Exception
            MsgBox("Creation of User Data Failed!")
            End
        End Try
        Try
            Me.createRepo()
            ProgressBar1.Value = 2
            Label2.Text = "Creating Default Repo Config..."
        Catch ex As Exception
            MsgBox("Creation of Repo Data Failed!")
        End Try
        Label2.Text = "Complete!"
        Me.Close()

    End Sub
    Sub createUser()
        If Not Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\") Then
            Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\")
        End If
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\User.xml"
        Dim writer As New XmlTextWriter(path, System.Text.Encoding.UTF8)
        Dim downloadpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\WSI Downloads\"
        writer.WriteStartDocument(True)
        writer.Formatting = Formatting.Indented
        writer.Indentation = 2
        writer.WriteStartElement("Root")
        createNodeUser(5, 1, downloadpath, writer)
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
    End Sub
    Sub createRepo()
        If Not Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\") Then
            Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\")
        End If
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\Repo.xml"
        Dim writer As New XmlTextWriter(path, System.Text.Encoding.UTF8)
        writer.WriteStartDocument(True)
        writer.Formatting = Formatting.Indented
        writer.Indentation = 2
        writer.WriteStartElement("Root")
        createNodeRepo("Dev-Network", "wsi.dev-network.com", "3306", "wsi_basic", "qwertyuiop", "wsi_wsi", writer)
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
    End Sub
    'User Data XML
    Private Sub createNodeUser(ByVal Build As String, ByVal Autoup As String, ByVal Downloads As String, ByVal writer As XmlTextWriter)
        writer.WriteStartElement("UserConfig")
        writer.WriteStartElement("Build")
        writer.WriteString(Build)
        writer.WriteEndElement()
        writer.WriteStartElement("AutoUpdate")
        writer.WriteString(Autoup)
        writer.WriteEndElement()
        writer.WriteStartElement("Downloads")
        writer.WriteString(Downloads)
        writer.WriteEndElement()
        writer.WriteEndElement()
    End Sub

    'Repo Data XML
    Private Sub createNodeRepo(ByVal Name As String, ByVal Host As String, ByVal Port As String, ByVal User As String, ByVal Password As String, ByVal Database As String, ByVal writer As XmlTextWriter)
        writer.WriteStartElement("RepoConfig")
        writer.WriteStartElement("RepoName")
        writer.WriteString(Name)
        writer.WriteEndElement()
        writer.WriteStartElement("RepoServer")
        writer.WriteString(Host)
        writer.WriteEndElement()
        writer.WriteStartElement("RepoPort")
        writer.WriteString(Port)
        writer.WriteEndElement()
        writer.WriteStartElement("RepoUser")
        writer.WriteString(User)
        writer.WriteEndElement()
        writer.WriteStartElement("RepoPass")
        writer.WriteString(Password)
        writer.WriteEndElement()
        writer.WriteStartElement("RepoDatabase")
        writer.WriteString(Database)
        writer.WriteEndElement()
        'End tag
        writer.WriteEndElement()
    End Sub
End Class
